package com.sgb.kjwl.view.ui.activity.self;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAddressBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.user.UserAddressEntity;
import com.sgb.kjwl.view.ui.adapter.self.UserAddressAdapter;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.CollectionAccountModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

/**
 *收款账号
 */
public class CollectionAccountActivity extends AppActivity {

    @Inject
    CollectionAccountModel viewModel;
    private ActivityAddressBinding binding;
    private UserAddressAdapter adapter;
    private List<UserAddressEntity> arrayList;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent)
                .selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void initView() {
        String tag = getIntent().getStringExtra("TAG");

        binding = DataBindingUtil.setContentView(CollectionAccountActivity.this, R.layout.activity_address);
        binding.include.titleView.setText("收款账号");
        binding.include.ivBack.setOnClickListener(this);
        binding.include.rightView.setText("新增收款账号");
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.rightView.setOnClickListener(this);
//        binding.tvAddAddress.setOnClickListener(this);

        adapter = new UserAddressAdapter(this, null);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_line)));
        binding.recyclerView.setAdapter(adapter);
        adapter.setonEditClick(new UserAddressAdapter.onEditClickListener() {
            @Override
            public void onEditClick(UserAddressEntity data) {
                AddressAddActivity.start(CollectionAccountActivity.this, data.getId());
            }

            @Override
            public void onDelClick(String id) {
                //viewModel.delIdAddress(id);
                //SwipeMenu.closeMenu();
                showDeleteDialog(id);
            }

            @Override
            public void isCheck(boolean isCheck, int position) {
                arrayList.get(position).setDefaultAdress(true);
                viewModel.overrideAddress(arrayList.get(position));
            }
        });

        adapter.setOnItemClick(new AdapterOnItemClick<UserAddressEntity>() {
            @Override
            public void onItemClick(UserAddressEntity userAddressEntity, int position) {
                if ("CHECK_ORDER".equals(tag)) {
                    EventBus.getDefault().postSticky(userAddressEntity);
                    finish();
                }
            }
        });

        binding.recyclerView.setLoadingMoreEnabled(false);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getAddressModel();
            }

            @Override
            public void onLoadMore() {

            }
        });

    }

    @Override
    protected void init() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.getAddressModel();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.right_view:
                AddressAddActivity.start(this, null);
                break;
        }
    }

    public void showAddress(List<UserAddressEntity> arrayList) {
        this.arrayList = arrayList;
        binding.recyclerView.refreshComplete();
        binding.nullView.setVisibility(arrayList.size() == 0 ? View.VISIBLE : View.GONE);
        adapter.setDatas(arrayList);
    }

    /**
     * 展示删除的dialog
     *
     * @param id
     */
    private void showDeleteDialog(final String id) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_message.setText("确定要删除地址吗？");

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //请求删除接口
                viewModel.delIdAddress(id);
                roundCornerDialog.dismiss();
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

}
